Page({
  data: {
    words: [],
    pageNum: 1,
    pageSize: 10,
    total: 0,
    loading: false,
    hasMore: true,
    isImageChange:false,
    isHotChange:false,
    imageSrc1:'/pages/img/887cdff6f914426f92d5d49407414eb2.png',
    imageSrc2:'/pages/img/00788e60b9304b8bb173db45508c2db7.png',
    favorites: [],
    clickCount:0,
    currentWord: null,
  },
  onLoad() {
    this.loadWordsList();
  },
  loadWordsList:function(){
    if(this.data.loading||!this.data.hasMore){
      return;
    }
    this.setData({loading:true});
    wx.request({
      url: 'http://81.70.199.174:8081/word/getAll',
      method:'GET',
      header:{
        'Authorization':getApp().globalData.token,
      },
      data:{
        pageNum:this.data.pageNum,
        pageSize:this.data.pageSize,
      },
      success:(res)=>{
        console.log(res.data);
        if(res.data.code===200){
          const newWords=res.data.data.words||[];
          const total=res.data.data.total||0;
          this.setData({
            words:this.data.pageNum==1?newWords:[...this.data.words,...newWords],
            total:total,
            hasMore:this.data.words.length + newWords.length < total,
          })
        }
      },
      complete:()=>{
        this.setData({loading:false,refreshing: false});
        wx.stopPullDownRefresh();
      }
    });
  },
  nextPage() {
    if (this.data.hasMore) {
      this.setData({ pageNum: this.data.pageNum + 1 });
      this.loadWordsList();
    }
  },
  goTo: function(e) {
    getApp().globalData.currentWord = this.data.words[e.currentTarget.dataset.index];
    wx.navigateTo({url: '/pages/detail/detail'});
  }
});